package com.hqhy.like.admin.account.dao.persist.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqhy.like.admin.account.dao.persist.mapper.UserMapper;
import com.hqhy.like.admin.account.dao.persist.repository.IUserRepository;
import com.hqhy.like.admin.account.pojo.entity.User;
import com.hqhy.like.admin.account.pojo.param.UserSearchParam;
import com.hqhy.like.admin.account.pojo.vo.UserListItemVO;
import com.hqhy.like.admin.account.pojo.vo.UserStandardVO;
import com.hqhy.like.common.pojo.vo.PageData;
import com.hqhy.like.common.util.PageInfoToPageDataConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 处理用户数据的存储库实现类
 */
@Slf4j
@Repository
public class UserRepositoryImpl implements IUserRepository {

    @Autowired
    private UserMapper userMapper;

    public UserRepositoryImpl() {
        log.info("创建存储库对象：UserRepositoryImpl");
    }

    @Override
    public int insert(User user) {
        log.debug("开始执行【插入用户】的数据访问，参数：{}", user);
        return userMapper.insert(user);
    }

    @Override
    public int deleteById(Long id) {
        log.debug("开始执行【根据ID删除用户】的数据访问，参数：{}", id);
        return userMapper.deleteById(id);
    }

    @Override
    public int updateById(User user) {
        log.debug("开始执行【更新用户】的数据访问，参数：{}", user);
        return userMapper.updateById(user);
    }

    @Override
    public int countByUsername(String username) {
        log.debug("开始执行【根据用户名统计用户的数量】的数据访问，参数：{}", username);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public int countByPhone(String phone) {
        log.debug("开始执行【根据手机号码统计用户的数量】的数据访问，参数：{}", phone);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public int countByPhoneAndNotId(String phone, Long userId) {
        log.debug("开始执行【统计匹配手机号码但非用户ID的用户数据的数量】的数据访问，手机号码：{}，用户ID：{}", phone, userId);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        queryWrapper.eq("id",userId);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public UserStandardVO getStandardById(Long id) {
        log.debug("开始执行【根据ID查询用户详情】的数据访问，参数：{}", id);
        return userMapper.getStandardById(id);
    }

    @Override
    public UserStandardVO getStandardByUsername(String username) {
        log.debug("开始执行【根据username查询用户详情】的数据访问，参数：{}", username);
        return userMapper.getStandardByUsername(username);
    }

    @Override
    public UserStandardVO getStandardByPhone(String phone) {
        log.debug("开始执行【根据phone查询用户详情】的数据访问，参数：{}", phone);
        return userMapper.getStandardByPhone(phone);
    }

    @Override
    public PageData<UserListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询用户列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<UserListItemVO> list = userMapper.list();
        PageInfo<UserListItemVO> pageInfo = new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public PageData<UserListItemVO> selectUserList(UserSearchParam userSearchParam, Integer pageNum, Integer pageSize) {
        log.debug("开始处理【搜索用户列表】的数据访问，用户参数：{} 页码：{}，每页记录数：{}",userSearchParam, pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<UserListItemVO> list = userMapper.selectUserList(userSearchParam);
        PageInfo<UserListItemVO> pageInfo = new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }
}